package me.samlss.lighter_demo;


import me.samlss.lighter.animator.AnimatorAttr;
import me.samlss.lighter.animator.CustomAnimatorValue;
import me.samlss.lighter.interceptor.BounceInterpolator;
import me.samlss.lighter.view.LighterView;
import ohos.aafwk.ability.Ability;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.MaskFilter;
import ohos.agp.render.Paint;
import ohos.agp.render.PathEffect;
import ohos.agp.utils.Color;
import ohos.global.resource.Resource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class LighterHelper {
    /**
     * 图片资源列表
     */
    private static final int[] PICTURES = new int[]{
            ResourceTable.Media_pic_1, ResourceTable.Media_pic_2,
            ResourceTable.Media_pic_3, ResourceTable.Media_pic_4,
            ResourceTable.Media_pic_5, ResourceTable.Media_pic_6,
            ResourceTable.Media_pic_7,
    };
    /**
     * 图片集合
     */
    public final static List<Integer>  sPictureList = new ArrayList<>();

    static {
        for (int i = 0; i < 20; i++){
            sPictureList.add(LighterHelper.PICTURES[i % LighterHelper.PICTURES.length]);
        }
    }

    private LighterHelper(){}
    /**
     * getDashPaint
     * @return Paint
     */
    public static Paint getDashPaint(){
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(20);

        paint.setPathEffect(new PathEffect(new float[]{20, 20}, 0));
        paint.setMaskFilter(new MaskFilter(20, MaskFilter.Blur.SOLID));
        return paint;
    }

    /**
     * getDiscretePaint
     * @return Paint
     */
    public static Paint getDiscretePaint(){
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(20);
        paint.setPathEffect(new PathEffect(10, 10));
        paint.setMaskFilter(new MaskFilter(20, MaskFilter.Blur.SOLID));
        return paint;
    }

    /**
     * 创建通用TipView
     * @param ability ability
     * @param drawableId drawableId
     * @param string string
     * @param lighterView lighterView
     * @return Tips 组件Component
     */
    public static Component createCommonTipView(Ability ability, int drawableId, String string, LighterView lighterView) {
        Component view = LayoutScatter.getInstance(ability).parse(ResourceTable.Layout_layout_tip_common, lighterView, false);
        Image imageView = (Image) view.findComponentById(ResourceTable.Id_iv_image);
        try {
            Resource resource = ability.getResourceManager().getResource(drawableId);
//            PixelMap pixelMap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
//            imageView.setPixelMap(pixelMap);
            Element element = new PixelMapElement(resource);
            imageView.setImageElement(element);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Text  textView = (Text) view.findComponentById(ResourceTable.Id_tv_text);
        textView.setText(string);
        return view;
    }

    /*public static void setupToolBarBackAction(final AppCompatActivity appCompatActivity, Toolbar toolbar){
        if (appCompatActivity == null
                || toolbar == null){
            return;
        }

        appCompatActivity.setSupportActionBar(toolbar);
        appCompatActivity.getSupportActionBar().setHomeButtonEnabled(true);
        appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                appCompatActivity.finish();//返回
            }
        });
    }*/

    /**
     * 获取Scale动画
     * @return AnimatorValue
     */
    public static AnimatorValue getScaleAnimation(){
        AnimatorAttr attr = new AnimatorAttr();
        attr.fromXDelta = 0.5f;
        attr.toXDelta = 1f;
        attr.fromYDelta = 0.5f;
        attr.toYDelta = 1f;
        attr.propertyValues = Arrays.asList(new String[]{"scaleX", "scaleY"});
        AnimatorValue animatorValue = new CustomAnimatorValue(attr, new BounceInterpolator());
        return animatorValue;
//        ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1f, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//        scaleAnimation.setDuration(500);
//        scaleAnimation.setInterpolator(new BounceInterpolator());
//        return scaleAnimation;
    }

    /**
     * 获取Translate动画
     * @return AnimatorValue
     */
    public static AnimatorValue getTranslateAnimation(){
        AnimatorAttr attr = new AnimatorAttr();
        attr.fromXDelta = -500;
        attr.toXDelta = 0;
        attr.propertyValues = Arrays.asList(new String[]{"translateX"});
        AnimatorValue animatorValue = new CustomAnimatorValue(attr, new BounceInterpolator());
        return animatorValue;
//        ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1f, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//        scaleAnimation.setDuration(500);
//        scaleAnimation.setInterpolator(new BounceInterpolator());
//        return scaleAnimation;
    }

}
